package com.example.chao.myphotosearch.view.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.example.chao.myphotosearch.R;
import com.example.chao.myphotosearch.adapters.MainPagerAdapter;
import com.example.chao.myphotosearch.view.fragments.BaseSwipeFragment;
import com.search.material.library.MaterialSearchView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    //    @Bind(R.id.swipe_layout)
//    SwipeRefreshLayout swipeLayout;
    @Bind(R.id.tabLayout)
    TabLayout tabLayout;
    @Bind(R.id.appBarLayout)
    AppBarLayout appBarLayout;
    @Bind(R.id.main_vp)
    ViewPager mainVp;
    @Bind(R.id.fab)
    FloatingActionButton fab;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.nav_view)
    NavigationView navigationView;
    @Bind(R.id.drawer_layout)
    DrawerLayout drawer;
    @Bind(R.id.search_view)
    MaterialSearchView searchView;

    private MainPagerAdapter adapter;

    //    private static MainActivity instance;
//    public static MainActivity getInstance(){
//        return instance;
//    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        instance = this;
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
        adapter = new MainPagerAdapter(this, getSupportFragmentManager());
        mainVp.setAdapter(adapter);
        tabLayout.setupWithViewPager(mainVp);
        setDrawerLayout();
//        swipeLayout.setProgressViewOffset(true, -20, 100);
        initAppBarSetting();
        initSearchView();
    }

    public void setDrawerLayout() {
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        navigationView.setCheckedItem(R.id.nav_main);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @OnClick(R.id.fab)
    public void clickFab(View view) {
        Log.d("clickFab", mainVp.getCurrentItem() + "");
        BaseSwipeFragment fragment = (BaseSwipeFragment) adapter.getFragment(mainVp.getCurrentItem());
        fragment.goToTop(0);
    }

    public void initAppBarSetting() {

        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
//                MainFragment fragment = (MainFragment) adapter.getFragment(0);
//                fragment.setMainSwipeEnable(i);
                if (i != 0) {
                    fab.hide();
                } else {
                    fab.show();
                }
            }
        });
    }

    @Override
    public void onBackPressed() {
        if(drawer.isDrawerOpen(GravityCompat.START)){
            drawer.closeDrawer(GravityCompat.START);
        }else if(searchView.isSearchOpen()){
            searchView.closeSearch();
        }else {
            super.onBackPressed();
        }
    }

    public void initSearchView() {
        searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Bundle bundle = new Bundle();
                bundle.putString("title", query);
                Intent intent = new Intent();
                intent.putExtra("search", bundle);
                intent.setClass(MainActivity.this, SearchActivity.class);
                startActivity(intent);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        MenuItem item = menu.findItem(R.id.action_search);
        searchView.setMenuItem(item);

        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       /* int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }*/

        return super.onOptionsItemSelected(item);
    }
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.nav_main) {
            mainVp.setCurrentItem(0);
        } else if (id == R.id.nav_search) {
            searchView.showSearch();
        }
        else if (id == R.id.nav_user) {
            startActivity(new Intent(this, UserActivity.class));
        }
//        else if (id == R.id.nav_setting) {
//            startActivity(new Intent(this, SettingActivity.class));
//        } else if (id == R.id.nav_share) {
//            openShare();
//        } else if (id == R.id.nav_send) {
//            FeedbackAgent agent = new FeedbackAgent(this);
//            agent.startFeedbackActivity();
//        }
        navigationView.setCheckedItem(id);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
